/*
 *  camera.c
 *  Ray Tracer
 *
 *  Created by Chirantan Ekbote on 10/5/10.
 *  Copyright 2010 Ohio State University. All rights reserved.
 *
 */

#include "../include/camera.h"

camera makeCamera(const vec3 eye, const vec3 coi, const vec3 up, const float ratio, 
				  const float view,const float dof, const int width, const int height) {
	camera temp;
	temp.pos = eye;
	temp.n = vec3UnitVector(vec3Sub(eye, coi));
	temp.u = vec3UnitVector(vec3Cross(up, temp.n));
	temp.v = vec3Cross(temp.n, temp.u);				// v is already normalized
	
	// Assume that the distance to the image plane is the focal length and calculate the position
	// of the bottom left pixel
	float planeHeight = dof * tan(view * 0.5f);	// Only want half the plane height
	float planeWidth = planeHeight * ratio;					// Half the plane width
	
	// Distance from camera to image plane is always the focal length
	vec3 c = vec3Sub(eye, vec3Mul(temp.n, dof));	
	
	// L = C - (u * w/2) - (v * h/2)
	temp.blpos = vec3Sub(vec3Sub(c, vec3Mul(temp.u, planeWidth)), 
						 vec3Mul(temp.v, planeHeight));
	
	// Calculate pixel width, height and center of bottom left pixel
	temp.pw = (2*planeWidth) / (float)(width);
	temp.ph = (2*planeHeight) / (float)(height);
	temp.blpos = vec3Add(vec3Add(temp.blpos, vec3Mul(temp.u, temp.pw * 0.5)), 
						 vec3Mul(temp.v, temp.ph * 0.5));
	
	temp.aspect = ratio;
	temp.fov = view;
	
	return temp;
}
